package me.tomato3017.plugins.epicmusic.region;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import me.tomato3017.plugins.epicmusic.EpicMusic;
import me.tomato3017.plugins.epicmusic.audio.AmbientMusicData;
import me.tomato3017.plugins.epicmusic.audio.MusicData;
import org.bukkit.Location;

/**
 *
 * @author Anthony Kirksey <Anthony.Kirksey@gmail.com>
 * Purpose: Determines the players current region. Retrieves it's playlist if requested.
 */
public class RegionManager {
    EpicMusic plugin;
    
    private HashMap<String,WorldData> worldData;
    private HashMap<String, ResidenceData> resData;
    private AmbientMusicData undergroundMusic;

    public RegionManager(EpicMusic plugin) {
        this.plugin = plugin;
        worldData = new HashMap<String, WorldData>();
        resData = new HashMap<String, ResidenceData>();
    }

    public RegionManager(EpicMusic plugin, AmbientMusicData undergroundMusic) {
        this.plugin = plugin;
        worldData = new HashMap<String, WorldData>();
        resData = new HashMap<String, ResidenceData>();
        this.undergroundMusic = undergroundMusic;
    }
    
    
    public RegionData GetRegion(Location loc){
        //TODO: search residence data to figure out location.
        
        String world = loc.getWorld().getName();
        return GetRegion(world);
    }
    
    public RegionData GetRegion(String region){
        if(region != null){
            if(worldData.containsKey(region)){
                return worldData.get(region);    
            }
            else{
                RegionData rData = new RegionData();
                return rData;
            }
        }
        return null;
    }
    
    public void AddRegion(WorldData region){
        if(region != null){
            worldData.put(region.getRegionName(), region);
        }
    }
    
    public void AddRegion(ResidenceData region){
        if(region != null){
            
            resData.put(region.getRegionName(), region);
        }
    }
    
    public synchronized List<MusicData> GetRegionPlaylist(String region){
        RegionData regionData = GetRegion(region);
        
        if( regionData == null ){
            return new ArrayList<MusicData>();
        }
        return GetRegion(region).getMusicPlaylist();
    }

    //TODO: use region data for per region underground noise
    public AmbientMusicData getUndergroundMusic(RegionData rData) {
        return undergroundMusic;
    }
    
    public void setUndergroundMusic(AmbientMusicData aMusic, RegionData rData){
        this.undergroundMusic = aMusic;
    }
}
